<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>예약 캘린더</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- 탑 인클루드 -->
<jsp:include page="/main/header.jsp"></jsp:include>

<script type="text/javascript">
function selectCheck(form){
    form.submit();
}
function monthDown(form){
 if(form.month.value>1){
     form.month.value--;
 }else {
     form.month.value=12;
     form.year.value--;
 }
 form.submit();
}
function monthUp(form){
 if(form.month.value<12){
     form.month.value++;
 }else {
     form.month.value=1;
     form.year.value++;
 }
 form.submit();
}
</script>
<body>
<%
//현재 날짜 정보 
Calendar cr = Calendar.getInstance();
int year = cr.get(Calendar.YEAR);
int month = cr.get(Calendar.MONTH);
int date = cr.get(Calendar.DATE);
String smonth = (month+1) <10 ? '0'+String.valueOf((month+1)):String.valueOf((month+1));
String sdate = date <10 ? '0'+String.valueOf(date):String.valueOf(date);
//오늘 날짜
String today = year + "-" +(month+1)+ "-"+date; 
String stoday = year + "-" + smonth + "-" + sdate;
//선택한 연도 / 월
String input_year = request.getParameter("year");
String input_month = request.getParameter("month");
  
if(input_month != null){
 month = Integer.parseInt(input_month)-1;
}
if(input_year != null){
 year = Integer.parseInt(input_year);
}
// 1일부터 시작하는 달력을 만들기 위해 오늘의 연도,월을 셋팅하고 일부분은 1을 셋팅한다.
cr.set(year, month, 1);
  
// 셋팅한 날짜로 부터 아래 내용을 구함
  
// 해당 월의 첫날를 구함
int startDate = cr.getMinimum(Calendar.DATE);
  
// 해당 월의 마지막 날을 구함
int endDate = cr.getActualMaximum(Calendar.DATE);
  
// 1일의 요일을 구함
int startDay = cr.get(Calendar.DAY_OF_WEEK);
  
int count = 0;
%>
<br><br><br><br><br><br><br><br><br>
<div class="content_inner">
   <div class="container">
      <div class="container_inner clearfix">
<form method="post" action="RMSCalendar.jsp" name="change" >
<table width="400" cellpadding="2" cellspacing="0" border="0" align="center">
 <tr>
   <td width="140" align="right"><input type="button" value="◁" onClick="monthDown(this.form)" style="width: 50px;"></td>
      <td width="120" align="center">
      <select name="year" onchange="selectCheck(this.form)" style="height: 30px;">
      <%
      for(int i=year-10;i<year+10;i++){
       String selected = (i == year)?"selected":"";
       String color = (i == year)?"#CCCCCC":"#FFFFFF";
         out.print("<option value="+i+" "+selected+" style=background:"+color+">"+i+"</option>");       
      }
      %>
      </select>
      <select name="month" onchange="selectCheck(this.form)" style="height: 30px;">
      <%
      for(int i=1;i<=12;i++){
       String selected = (i == month+1)?"selected":"";
       String color = (i == month+1)?"#CCCCCC":"#FFFFFF";
         out.print("<option value="+i+" "+selected+" style=background:"+color+">"+i+"</option>");       
      }
      %>
      </select></td>
      <td width="140"><input type="button" value="▷" onClick="monthUp(this.form)" style="width: 50px;"></td>
    </tr>
    <tr>
      <td align="right" colspan="3">
      <a href="RMSListDB.bo"><font size="2">전체 예약</font></a>&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="RMSSelectDB.bo?TRANDATE=<%=stoday %>&select=2"><font size="2">오늘 예약: <%=stoday %></font></a></td>
    </tr>
</table> 
</form>        
<table width="700" height="450" cellpadding="2" cellspacing="0" align="center" style="margin-right: 100px;">
 <tr height="30">
  <td><font size="2">일</font></td>
  <td><font size="2">월</font></td>
  <td><font size="2">화</font></td>
  <td><font size="2">수</font></td>
  <td><font size="2">목</font></td>
  <td><font size="2">금</font></td>
  <td><font size="2">토</font></td>
 </tr>
 <tr height="30">
<%
for (int i=1;i<startDay;i++){
 count++;
%>
        <td>&nbsp;</td>
<% 
}
for (int i=startDate;i<=endDate;i++){
 String bgcolor = (today.equals(year+"-"+(month+1)+"-"+i))? "#CCCCCC" : "#FFFFFF";
 String color;
	if(count%7==0)
		color="red";
	else if(count%7==6)
		color="blue";
	else
		color="black";
 count++;
 String s_i = i <10 ? '0'+String.valueOf(i):String.valueOf(i);
%> 
  <td style="background-color: <%=bgcolor %>;"><a href="RMSSelectDB.bo?TRANDATE=<%=year+"-"+smonth+"-"+s_i %>&select=2"><font size="2" color=<%=color %>><%=i %></font></a></td>
<%
  if(count%7 == 0 && i < endDate){
%> 
 </tr>
 <tr height="30">
<%
  }
}
while(count%7 != 0){
%>
       <td>&nbsp;</td>
<% 
count++;
 }
%>
</tr>  
</table>
</div></div></div>
</body>
</html> 